<template>
  <a-modal
    title="已发消息列表"
    style="top: 20px;"
    :width="900"
    v-model="isShow"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :footer="null"
  >
    <div class="table-operator" style="margin-bottom: 18px">
      <a-button type="danger" style="margin-left: 8px" @click="deleteBatchHanlde" :disabled="selectedRowKeys.length == 0">删除</a-button>
    </div>
    <s-table
      v-if="visible"
      ref="table"
      size="middle"
      :columns="columns"
      :data="loadData"
      rowKey="id"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      showPagination="auto"
    >

      <template slot="subject" slot-scope="text" >
        <a-tooltip placement="topLeft">
          <template slot="title">
            {{ delHtmlTag(text) }}
          </template>
          <span v-html="text"></span>
        </a-tooltip>
      </template>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="()=>(openMessageDetail(record))" alt="查看" title="查看" style="margin-left:8px;"><a-icon type="search" :style="{fontSize:'16px'}"/></a>
          <a-popconfirm
            title="确定要删除吗?"
            @confirm="() => deleteHanlde(record.id)"
          >
            <a alt="删除" title="删除" style="margin-left:8px"><a-icon type="close" :style="{fontSize:'16px'}"/></a>
          </a-popconfirm>

        </template>
      </span>
    </s-table>
    <message-detail ref="messageDetail"/>
  </a-modal>
</template>
<script>
import { STable } from '@/components'
import MessageDetail from '@/components/MessageNotice/MessageDetail'
import Ellipsis from '@/components/Ellipsis'
import { getMessageSendList, delMessage } from '@/api/system'
import { envelopeOpen, envelope } from '@/core/icons'
export default {
  name: 'MessageListModal',
  components: {
    STable,
    envelopeOpen,
    envelope,
    MessageDetail,
    Ellipsis
  },
  props: {
    visible: {
      type: Boolean,
      required: true,
      default: false
    }
  },
  data () {
    return {

      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      mdl: {},
      confirmLoading: false,
       columns: [

        {
          title: '标题',
          dataIndex: 'subject',
          width: '30%',
          scopedSlots: { customRender: 'subject' },
          ellipsis: true
        },
        {
          title: '发送人',
          dataIndex: 'formUser'
        },
        {
          title: '发送时间',
          dataIndex: 'createTime',
          sorter: true
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
     loadData: parameter => {
        return getMessageSendList(Object.assign(parameter))
      },
      options: {
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  computed: {
    isShow: {
      get () {
        return this.visible
      },
      set (val) {
        this.$emit('update:visible', val)
      }
    }
  },
  beforeCreate () {
  },
  created () {

  },
  methods: {
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    handleSubmit () {

    },
    deleteBatchHanlde () {
      const that = this
      this.$confirm({
        title: '确定要删除吗？删除后无法恢复！',
        okType: 'danger',
        onOk () {
           that.deleteHanlde(that.selectedRowKeys.join())
        }
      })
    },
    deleteHanlde (ids) {
      delMessage(ids, 'fromUser').then(res => {
        if (res.status === 1) {
            this.$message.success(res.message)
            this.$refs.table.refresh(true)
            this.$refs.table.clearSelected()
            this.refresh()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    openMessageDetail (mdl) {
      this.$refs.messageDetail.open(mdl)
    },
    refresh () {
      this.$emit('ok')
    },
    delHtmlTag (str) {
      return str.replace(/<[^>]+>/g, '')// 去掉所有的html标记
    }
  },
  watch: {
  }
}
</script>

<style scoped>
</style>
